package com.example.zzl.adapter.SearchFragment;

import android.content.Context;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.app.MyApplication;
import com.example.zzl.model.bean.SearchFragment.SearchResultData;
import com.example.zzl.utils.LruImageCache;

import java.util.ArrayList;

/**
 * Created by zhang on 2017/2/10.
 */

public class GridViewSRAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<SearchResultData.ResultBean.InfoListBean> list;
    private ImageLoader imageLoader;

    public GridViewSRAdapter(Context context, ArrayList<SearchResultData.ResultBean.InfoListBean> list) {
        this.context = context;
        this.list = list;
        imageLoader = new ImageLoader(MyApplication.queue, LruImageCache.getInstance());
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if(view == null){
            view = View.inflate(context, R.layout.gv_sr_item,null);
            vh = new ViewHolder();
            vh.iv = (ImageView)view.findViewById(R.id.iv_gv_sr_1);
            vh.tv_1 = (TextView)view.findViewById(R.id.tv_gv_sr_2);
            vh.tv_2 = (TextView)view.findViewById(R.id.tv_gv_sr_1);
            vh.tv_3 = (TextView)view.findViewById(R.id.tv_gv_sr_price_1);
            vh.tv_4 = (TextView)view.findViewById(R.id.tv_gv_sr_price_2);

            view.setTag(vh);
        }else{
            vh = (ViewHolder) view.getTag();
        }
        vh.tv_1.setText(list.get(i).getInfoTitle());
        vh.tv_2.setText(list.get(i).getMallName()+"供货");
        vh.tv_3.setText("¥"+String.valueOf(list.get(i).getMinPriceRMB()));
        if(list.get(i).getMaxPriceRMB() == list.get(i).getMinPriceRMB()){
            vh.tv_4.setText("");
        }else {
            vh.tv_4.setText("¥" + String.valueOf(list.get(i).getMaxPriceRMB()));
        }
        vh.tv_4.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG );
        loadImgByVolley(list.get(i).getMainImage(),vh.iv);
        return view;
    }

    private void loadImgByVolley(String string, ImageView ivImage) {
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(ivImage,
                R.drawable.ic_launcher, R.drawable.ic_launcher);
        imageLoader.get(string, listener);
    }

    class ViewHolder{
        ImageView iv;
        TextView tv_1,tv_2,tv_3,tv_4;
    }
}
